package com.forum.module.blog.controller.article_tag;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.blog.controller.article.vo.ArticlePageReqVO;
import com.forum.module.blog.controller.article.vo.ArticleRespVO;
import com.forum.module.blog.controller.article.vo.ArticleSaveReqVO;
import com.forum.module.blog.controller.article_tag.vo.ArticleTagPageReqVO;
import com.forum.module.blog.controller.article_tag.vo.ArticleTagRespVO;
import com.forum.module.blog.controller.article_tag.vo.ArticleTagSaveReqVO;
import com.forum.module.blog.convert.article.ArticleConvert;
import com.forum.module.blog.convert.article_tag.ArticleTagConvert;
import com.forum.module.blog.dal.dataobject.article.ArticleDO;
import com.forum.module.blog.dal.dataobject.article_tag.ArticleTagDO;
import com.forum.module.blog.service.article_tag.ArticleTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/4 19:19
 */
@Tag(name = "博客管理 - 文章标签关联")
@RestController
@RequestMapping("/blog/article-tag")
public class ArticleTagController {
    private final ArticleTagService articleTagService;

    public ArticleTagController(ArticleTagService articleTagService) {
        this.articleTagService = articleTagService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增文章标签关联")
    public DataResult<Long> createArticleTag(@RequestBody ArticleTagSaveReqVO reqVO) {
        Long id = articleTagService.createArticleTag(reqVO);
        return success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新文章标签关联")
    public DataResult<Boolean> updateArticleTag(@RequestBody ArticleTagSaveReqVO reqVO) {
        articleTagService.updateArticleTag(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章标签关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<Boolean> deleteArticleTag(@RequestParam("id") Long id) {
        articleTagService.deleteById(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询文章标签关联")
    public DataResult<PageResult<ArticleTagRespVO>> page(@RequestBody @Valid ArticleTagPageReqVO pageReqVO) {
        // 获得文章分页列表
        PageResult<ArticleTagDO> pageResult = articleTagService.getArticleTagPage(pageReqVO);

        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        return success(new PageResult<>(ArticleTagConvert.INSTANCE.covertList(pageResult.getList()), pageResult.getTotal()));
    }

    @GetMapping("/get")
    @Operation(summary = "获取博客文章标签关联详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<ArticleTagRespVO> getArticleTag(@RequestParam("id") Long id) {
        ArticleTagDO articleTagDO = articleTagService.getArticleTag(id);
        if (articleTagDO == null) {
            return success(null);
        }
        return success(ArticleTagConvert.INSTANCE.covert(articleTagDO));
    }
}
